var layer;
var form;
var table;
var treeSelect;
var treeTable;
var temp_name;

/**
 * layui 组件引入
 * @returns
 */
layui.use([ 'layer', 'form', 'table', 'jquery' , "treeSelect", "treeTable", "iconSelected", "dropdown"], function() {
	layer = layui.layer;
	form = layui.form;
	table = layui.table;
	treeSelect = layui.treeSelect;
	treeTable = layui.treeTable;

	// 加载字典=============================================================================================
	getDictLabel("org_type", "#org_type", "type", true);
	getDictLabel("use_state", "#state", "state", true);

	// 执行一个 table 实例=============================================================================================
	var treeTablParam = treeTable.render({
		elem : '#data_table',
		id : 'data_table',
		url : ctxPath+'sys/organization/selectTreeTable',
		//height:'full-120', // 自动填充可视高度
		page: false,
		toolbar : '#toolberLeft',
		treeColIndex: 1,	// 树形图标显示在第几列
		treeSpid: 0,		// 最上级的父级id
		treeIdName: 'oid',	// id字段的名称
		treePidName: 'parentId',// pid字段的名称
		treeDefaultClose: false,// 是否默认折叠
		treeLinkage: true,		// 父级展开时是否自动展开所有子级
		cols : [[
			{type : 'checkbox', fixed : 'left', hide: true},
			{field : 'oname', title : '机构名称', align : 'left', minWidth: '150', hide: setColumnHide("oname")},
			//{field : 'oid', title : 'ID', align : 'center', minWidth: '80', hide: setColumnHide("oid")},
			{field : 'abbreviation', title : '机构简称', align : 'center', minWidth: '80', hide: setColumnHide("abbreviation")},
			{field : 'num', title : '机构编码', align : 'center', minWidth: '80', hide: setColumnHide("num")},
			{field : 'state', title : '状态', align : 'center', minWidth: '80', hide: setColumnHide("state"),
				templet: function (d) {
					return '<input type="checkbox" oid="'+d.oid+'" '+(d.oid==1?"disabled":"")+' '+(d.oid==oid?"disabled":"")+' '+(d.state==1?"checked":"")+' name="state" lay-skin="switch" lay-filter="stateSwitch" lay-text="启用|禁用">';
				}
			},
			{field : 'sort', title : '排序号', align : 'center', minWidth: '80', hide: setColumnHide("sort")},
			{field : 'type', title : '类型', align : 'center', minWidth: '80', hide: setColumnHide("type"), templet: function (d){return getDictText("org_type",d.type)}},
			{fixed : isRight(), title : '操作', align : 'center', minWidth: '300', toolbar : '#toolRow'}
		]],
		done: function () {
			$("#serarch_name").val(temp_name);
			form.render();
		}
	});

	// 头部左侧事件=============================================================================================
	table.on('toolbar(data_table)',  function(obj) {
		if (obj.event === 'search') {
			method.laodTreeTabl();
		}else if(obj.event === 'add'){
			$("#add_parentId").css("display", "block");
			openWindowRB('data_form', '机构添加', '500px', '100%', false, 0, false, false, null, 0.2, false, function(){
				loadTreeOrganization(0);// 下拉树形机构
			});
		}else if(obj.event === 'expand'){
			treeTable.expandAll('#data_table');
		}else if(obj.event === 'fold'){
			treeTable.foldAll('#data_table');
		}
	})

	// 函数对象=============================================================================================
	var method = {
		laodTreeTabl: function(){
			temp_name = $("#serarch_name").val();
			treeTablParam.where = {"oname": temp_name};
			treeTablParam = treeTable.render(treeTablParam);
		}
	}

	// 监听行工具事件=============================================================================================
	table.on('tool(data_table)', function(obj) {
		var data = obj.data;
		if (obj.event === 'add') {
			$("#add_parentId").css("display", "block");
			openWindowRB('data_form', '机构添加', '500px', '100%', false, 0, false, false, null, 0.2, false, function(){
				loadTreeOrganization(data.oid);
			});
		}else if (obj.event === 'detail'){
			if(oid == data.oid){
				$("#add_parentId").css("display", "none");
			}else {
				$("#add_parentId").css("display", "block");
			}
			openWindowRB('data_form', '机构查看', '500px', '100%', false, 0, false, false, data, 0.2, false, function(res){
				loadTreeOrganization(data.parentId);
			});
		} else if(obj.event === 'edit'){
			if(oid == data.oid){
				$("#add_parentId").css("display", "none");
			}else {
				$("#add_parentId").css("display", "block");
			}
			openWindowRB('data_form', '机构编辑', '500px', '100%', false, 0, false, false, data, 0.2, false, function(res){
				loadTreeOrganization(data.parentId);
			});
		} else if(obj.event === 'del'){
			deleteRow(table, 'data_table', {'oid':data.oid}, ctxPath+'sys/organization/delete'+"?oid="+data.oid, function(res){
				if (res.success) {
					method.laodTreeTabl();
				}
			});
		}
	});

	// 提交项目=============================================================================================
	form.on('submit(submitForm)', function(data) {
		saveData(table, 'data_table', data.field, ctxPath+'sys/organization/save', '#data_form ', function(res){
			if (res.success) {
				method.laodTreeTabl();
			}
		});
		return false;
	})

	// 开关=============================================================================================
	form.on('switch(stateSwitch)', function(data) {
		var oid = $(this).attr('oid');
		var state = data.elem.checked?"1":"0";
		getAjax(ctxPath+"sys/organization/updateState", {oid: oid, state: state}, 'post', function (res) {
			showTips(res.msg);
			if (res.success) {
				method.laodTreeTabl();
			}
		})
		return false;
	})



})



/**
 * ---------------------------------------------------------------------------------------------------------------------
 * 获取所有项目-树形
 */
function loadTreeOrganization(id){
	$.get(ctxPath+"sys/organization/selectTreeAll", {}, function (result) {
		if (result.success){
			treeSelect.destroy('parentTree');
			// 参考：https://wujiawei0926.gitee.io/treeselect/docs/doc.html
			treeSelect.render({
				elem: '#parentTree',
				data:  '',
				list: result.data,
				type: 'get',
				placeholder: '请选择机构',
				search: true,
				style: {
					folder: {
						enable: false // 图标
					},
					line: {
						enable: true  // 线
					}
				},
				click: function(d){
					console.log(d);
				},
				success: function (d) {
					// 选中节点，根据id筛选
					treeSelect.checkNode('parentTree', id);
					// 刷新树结构
					treeSelect.refresh('parentTree');
				}
			});
		}
	})
}






